

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>The patterns module &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="index.html">pyamp v1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-pyamp.patterns">
<span id="the-patterns-module"></span><h1>The patterns module<a class="headerlink" href="#module-pyamp.patterns" title="Permalink to this headline">¶</a></h1>
<p>The patterns module contains modules pertaining to implementing
common design patterns.</p>
<div class="section" id="the-borg-class">
<h2>The Borg class<a class="headerlink" href="#the-borg-class" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="pyamp.patterns.Borg">
<em class="property">class </em><tt class="descclassname">pyamp.patterns.</tt><tt class="descname">Borg</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Borg" title="Permalink to this definition">¶</a></dt>
<dd><p>The Borg class implements the Borg design pattern which provides
a singleton like pattern for Python.</p>
<p>A Borg object can be accessed by calling the <a class="reference internal" href="#pyamp.patterns.Borg.getInstance" title="pyamp.patterns.Borg.getInstance"><tt class="xref py py-func docutils literal"><span class="pre">getInstance()</span></tt></a> function.
This functions returns an instance of the Borg class which stores its state
between successive calls to get the Borg object.</p>
<p>Instances of the Borg class can implement the <a class="reference internal" href="#pyamp.patterns.Borg.init" title="pyamp.patterns.Borg.init"><tt class="xref py py-func docutils literal"><span class="pre">init()</span></tt></a> function which
is called when the Borg class is first created and initialized. This allows
the concrete Borg classes to set up the initial state for their object.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Example</span><span class="p">(</span><span class="n">Borg</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;x&#39;</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">First</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># An example of passing a value to the Borg instance</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">Example</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">&quot;First, b.x:&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x</span>

<span class="k">class</span> <span class="nc">Second</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Optionally could use Borg.getInstance()</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">Example</span><span class="o">.</span><span class="n">getInstance</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Second, b.x:&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x</span>
        <span class="n">b</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">500</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">First</span><span class="p">()</span>
    <span class="n">Second</span><span class="p">()</span>

    <span class="n">b</span> <span class="o">=</span> <span class="n">Example</span><span class="o">.</span><span class="n">getInstance</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&quot;Third, b.x:&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x</span>

    <span class="c"># Prints:</span>
    <span class="c">#    First, b.x: 200</span>
    <span class="c">#    Second, b.x: 200</span>
    <span class="c">#    Third, b.x: 500</span>
</pre></div>
</div>
<ul class="simple">
<li>args &#8211; The arguments</li>
<li>kwargs &#8211; The keyword arguments</li>
</ul>
<dl class="classmethod">
<dt id="pyamp.patterns.Borg.getInstance">
<em class="property">classmethod </em><tt class="descname">getInstance</tt><big>(</big><big>)</big><a class="headerlink" href="#pyamp.patterns.Borg.getInstance" title="Permalink to this definition">¶</a></dt>
<dd><p>Get an instance of the Borg object.</p>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.Borg.getPrivateAttr">
<tt class="descname">getPrivateAttr</tt><big>(</big><em>name</em>, <em>default=None</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Borg.getPrivateAttr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value of a private class attribute.</p>
<ul class="simple">
<li>name &#8211; The name of the private attribute</li>
<li>default &#8211; The value returned if the attribute does not exist</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.Borg.init">
<tt class="descname">init</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Borg.init" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is called the first time the class is initialized,
and should be overridden by concrete subclasses.</p>
<ul class="simple">
<li>args &#8211; The arguments</li>
<li>kwargs &#8211; The keyword arguments</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.Borg.setPrivateAttr">
<tt class="descname">setPrivateAttr</tt><big>(</big><em>name</em>, <em>value</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Borg.setPrivateAttr" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value of a private class attribute.</p>
<ul class="simple">
<li>name &#8211; The name of the private attribute</li>
<li>value &#8211; The value to set the attribute to</li>
</ul>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="the-enum-class">
<h2>The Enum class<a class="headerlink" href="#the-enum-class" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="pyamp.patterns.Enum">
<em class="property">class </em><tt class="descclassname">pyamp.patterns.</tt><tt class="descname">Enum</tt><a class="headerlink" href="#pyamp.patterns.Enum" title="Permalink to this definition">¶</a></dt>
<dd><p>The Enum class provides the ability to create a class which acts as an
enumeration. Class properties can be set as the enumeration values, and a
dictionary of the entire enumeration keys and values can be retrieved.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">Prop1</span> <span class="o">=</span> <span class="s">&quot;This is a string property&quot;</span>
    <span class="n">Prop2</span> <span class="o">=</span> <span class="mi">123</span>
    <span class="n">Another</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">AnyIdName</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;test&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">}</span>
    <span class="n">ListId</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
</pre></div>
</div>
<p>Calling Test.get(), returns:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span> <span class="s">&quot;Prop1&quot;</span><span class="p">:</span> <span class="s">&quot;This is a string property&quot;</span><span class="p">,</span>
  <span class="s">&quot;Prop2&quot;</span><span class="p">:</span> <span class="mi">123</span><span class="p">,</span>
  <span class="s">&quot;Another&quot;</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
  <span class="s">&quot;AnyIdName&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;test&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">},</span>
  <span class="s">&quot;ListId&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Create an Enum.</p>
<dl class="classmethod">
<dt id="pyamp.patterns.Enum.get">
<em class="property">classmethod </em><tt class="descname">get</tt><big>(</big><big>)</big><a class="headerlink" href="#pyamp.patterns.Enum.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the list of configured enumeration values.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="decorators">
<h2>Decorators<a class="headerlink" href="#decorators" title="Permalink to this headline">¶</a></h2>
<p>The following describes various functions that can be used to decorate
functions to perform particular tasks.</p>
<div class="section" id="the-listproperty-decorator">
<h3>The listProperty decorator<a class="headerlink" href="#the-listproperty-decorator" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="pyamp.patterns.listProperty">
<tt class="descclassname">pyamp.patterns.</tt><tt class="descname">listProperty</tt><big>(</big><em>propName</em>, <em>values</em><big>)</big><a class="headerlink" href="#pyamp.patterns.listProperty" title="Permalink to this definition">¶</a></dt>
<dd><p>The listProperty function returns a decorator that appends a list of
values (to the property with the given name) to a function&#8217;s attributes.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@listProperty</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s">&quot;two&quot;</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">example</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="nd">@listProperty</span><span class="p">(</span><span class="s">&quot;prop&quot;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="nd">@listProperty</span><span class="p">(</span><span class="s">&quot;prop&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s">&quot;another property&quot;</span><span class="p">])</span>
<span class="nd">@listProperty</span><span class="p">(</span><span class="s">&quot;prop&quot;</span><span class="p">,</span> <span class="p">[</span><span class="bp">True</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">example2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="c"># Prints: [1, &quot;two&quot;, 3]</span>
<span class="k">print</span> <span class="n">example</span><span class="o">.</span><span class="n">test</span>

<span class="c"># Prints: [1, &quot;another property&quot;, True]</span>
<span class="k">print</span> <span class="n">example2</span><span class="o">.</span><span class="n">prop</span>
</pre></div>
</div>
<ul class="simple">
<li>propName &#8211; The name of the property to set on the decorated function</li>
<li>values &#8211; The value, or list of values, to append</li>
</ul>
</dd></dl>

</div>
</div>
<div class="section" id="interfaces">
<h2>Interfaces<a class="headerlink" href="#interfaces" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="the-interface-class">
<h2>The Interface class<a class="headerlink" href="#the-interface-class" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="pyamp.patterns.Interface">
<em class="property">class </em><tt class="descclassname">pyamp.patterns.</tt><tt class="descname">Interface</tt><a class="headerlink" href="#pyamp.patterns.Interface" title="Permalink to this definition">¶</a></dt>
<dd><p>The Interface class provides to ability to identify specific class
functions to be &#8216;interface&#8217; functions.</p>
<p>This class contains a class property called InterfaceProperty which defines
the name of the property used to identify interface functions.</p>
<p>This class provides a <a class="reference internal" href="#pyamp.patterns.Interface.interface" title="pyamp.patterns.Interface.interface"><tt class="xref py py-func docutils literal"><span class="pre">interface()</span></tt></a> function which uses the
InterfaceProperty property to create an implementation specific decorator
which can be used to decorate functions that should identified as interface
functions.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Example</span><span class="p">(</span><span class="n">Interface</span><span class="p">):</span>
    <span class="n">InterfaceProperty</span> <span class="o">=</span> <span class="s">&quot;ExampleInterfaceProperty&quot;</span>

    <span class="nd">@Example.interface</span>
    <span class="k">def</span> <span class="nf">testPrint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="k">print</span> <span class="s">&quot;Message:&quot;</span><span class="p">,</span> <span class="n">message</span>
</pre></div>
</div>
<p>This the above example the <tt class="xref py py-func docutils literal"><span class="pre">testPrint()</span></tt> function has been decorated
and is thus an interface function.</p>
<dl class="method">
<dt id="pyamp.patterns.Interface.connect">
<tt class="descname">connect</tt><big>(</big><em>parent</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Interface.connect" title="Permalink to this definition">¶</a></dt>
<dd><p>Connect to a parent&#8217;s interface methods.</p>
<p>Note: This should be overriden by concrete Interfaces</p>
<ul class="simple">
<li>parent &#8211; The parent interface object</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.Interface.getInterfaceFunctions">
<tt class="descname">getInterfaceFunctions</tt><big>(</big><big>)</big><a class="headerlink" href="#pyamp.patterns.Interface.getInterfaceFunctions" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a dictionary of the interface function names mapped
to the actual functions.</p>
</dd></dl>

<dl class="classmethod">
<dt id="pyamp.patterns.Interface.interface">
<em class="property">classmethod </em><tt class="descname">interface</tt><big>(</big><em>function</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Interface.interface" title="Permalink to this definition">¶</a></dt>
<dd><p>A decorator for functions to make them interface functions for
this Interface instance.</p>
<ul class="simple">
<li>function &#8211; The function to decorate</li>
</ul>
</dd></dl>

<dl class="classmethod">
<dt id="pyamp.patterns.Interface.isInterface">
<em class="property">classmethod </em><tt class="descname">isInterface</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#pyamp.patterns.Interface.isInterface" title="Permalink to this definition">¶</a></dt>
<dd><p>Determine if the given object is an instance of the interface.</p>
<ul class="simple">
<li>obj &#8211; The object</li>
</ul>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="the-passthrough-class">
<h2>The PassThrough class<a class="headerlink" href="#the-passthrough-class" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="pyamp.patterns.PassThrough">
<em class="property">class </em><tt class="descclassname">pyamp.patterns.</tt><tt class="descname">PassThrough</tt><a class="headerlink" href="#pyamp.patterns.PassThrough" title="Permalink to this definition">¶</a></dt>
<dd><p>The PassThrough <a class="reference internal" href="#pyamp.patterns.Interface" title="pyamp.patterns.Interface"><tt class="xref py py-class docutils literal"><span class="pre">Interface</span></tt></a> provides the ability for a child
PassThrough object to connect functions of the same name to the parent&#8217;s
<a class="reference internal" href="#pyamp.patterns.Interface" title="pyamp.patterns.Interface"><tt class="xref py py-class docutils literal"><span class="pre">Interface</span></tt></a> functions.</p>
<p>When a child class connects to the parent&#8217;s interface functions, all calls
to the child&#8217;s interface functions are instead redirected to the parent&#8217;s
functions of the same name.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Parent</span><span class="p">(</span><span class="n">PassThrough</span><span class="p">):</span>
    <span class="nd">@PassThrough.interface</span>
    <span class="k">def</span> <span class="nf">testPrint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="k">print</span> <span class="s">&quot;Parent message:&quot;</span><span class="p">,</span> <span class="n">message</span>

<span class="k">class</span> <span class="nc">Child</span><span class="p">(</span><span class="n">PassThrough</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="n">parent</span> <span class="o">=</span> <span class="n">Parent</span><span class="p">()</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">Child</span><span class="p">()</span>
<span class="n">child</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>

<span class="c"># Prints:</span>
<span class="c">#    &quot;Parent message:&quot;, this is a message</span>
<span class="n">child</span><span class="o">.</span><span class="n">testPrint</span><span class="p">(</span><span class="s">&quot;this is a message&quot;</span><span class="p">)</span>
</pre></div>
</div>
<dl class="method">
<dt id="pyamp.patterns.PassThrough.connect">
<tt class="descname">connect</tt><big>(</big><em>parent</em><big>)</big><a class="headerlink" href="#pyamp.patterns.PassThrough.connect" title="Permalink to this definition">¶</a></dt>
<dd><p>Connect the child to the parent&#8217;s interface functions.</p>
<ul class="simple">
<li>parent &#8211; The parent Interface</li>
</ul>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The patterns module</a><ul>
<li><a class="reference internal" href="#the-borg-class">The Borg class</a></li>
<li><a class="reference internal" href="#the-enum-class">The Enum class</a></li>
<li><a class="reference internal" href="#decorators">Decorators</a><ul>
<li><a class="reference internal" href="#the-listproperty-decorator">The listProperty decorator</a></li>
</ul>
</li>
<li><a class="reference internal" href="#interfaces">Interfaces</a></li>
<li><a class="reference internal" href="#the-interface-class">The Interface class</a></li>
<li><a class="reference internal" href="#the-passthrough-class">The PassThrough class</a></li>
</ul>
</li>
</ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/patterns.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="index.html">pyamp v1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>